package com.xqboss.apps.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xqboss.apps.domain.sys.SysFeedbackRecord;
import com.xqboss.apps.dto.sys.WebSysFeedbackRecordDto;
import com.xqboss.apps.service.sys.SysFeedbackRecordService;
import com.xqboss.common.constant.HttpStatus;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 反馈记录接口
 *
 * @author wangcong
 * @date 2023-09-04
 */
@Api(tags = {"反馈记录"})
@RestController
@RequestMapping("/apps/feedback")
public class SysFeedbackRecordController extends BaseController {
    @Autowired
    private SysFeedbackRecordService sysFeedbackRecordService;

    @ApiOperation("条件查询")
    @GetMapping("/list")
    public R<List<SysFeedbackRecord>> findBy(WebSysFeedbackRecordDto dto) {
        LambdaQueryWrapper<SysFeedbackRecord> wp = new LambdaQueryWrapper<>();
        wp.eq(SysFeedbackRecord::getUserId, getUserId());
        wp.orderByDesc(SysFeedbackRecord::getCreateTime);
        dto.startPage();
        List<SysFeedbackRecord> list = sysFeedbackRecordService.list(wp);
        return R.ok(list);
    }

    @ApiOperation("根据主键查询详情")
    @GetMapping("/info/{id}")
    public R<SysFeedbackRecord> findByKey(@PathVariable("id")
                                          @ApiParam(name = "id", value = "主键", required = true) Long id) {
        return R.ok(sysFeedbackRecordService.getById(id));
    }

    @ApiOperation("新增反馈记录")
    @PostMapping("/add")
    public R add(@RequestBody SysFeedbackRecord dto) {
        LambdaQueryWrapper<SysFeedbackRecord> wq = new LambdaQueryWrapper<>();
        wq.eq(SysFeedbackRecord::getUserId, getUserId());
        wq.like(SysFeedbackRecord::getCreateTime, DateUtils.getDate());
        long count = sysFeedbackRecordService.count(wq);
        if (count >= 10) {
            return R.fail(HttpStatus.FEEDBACK_UPPER_LIMIT,"反馈次数上限");
        }
        dto.setUserId(getUserId());
        sysFeedbackRecordService.save(dto);
        return R.ok(dto.getId());
    }
}
